package cn.edu.scau.cmi.hombio.service;

import cn.edu.scau.cmi.hombio.dao.PigTypeDAO;
import cn.edu.scau.cmi.hombio.dao.SaleTaskQualityDAO;
import cn.edu.scau.cmi.hombio.dao.TaskDAO;

import cn.edu.scau.cmi.hombio.domain.PigType;
import cn.edu.scau.cmi.hombio.domain.SaleTaskQuality;
import cn.edu.scau.cmi.hombio.domain.Task;

import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;

/**
 * Spring service that handles CRUD requests for SaleTaskQuality entities
 * 
 */

@Service("SaleTaskQualityService")

@Transactional
public class SaleTaskQualityServiceImpl implements SaleTaskQualityService {

	/**
	 * DAO injected by Spring that manages PigType entities
	 * 
	 */
	@Autowired
	private PigTypeDAO pigTypeDAO;

	/**
	 * DAO injected by Spring that manages SaleTaskQuality entities
	 * 
	 */
	@Autowired
	private SaleTaskQualityDAO saleTaskQualityDAO;

	/**
	 * DAO injected by Spring that manages Task entities
	 * 
	 */
	@Autowired
	private TaskDAO taskDAO;

	/**
	 * Instantiates a new SaleTaskQualityServiceImpl.
	 *
	 */
	public SaleTaskQualityServiceImpl() {
	}

	/**
	 * Delete an existing PigType entity
	 * 
	 */
	@Transactional
	public SaleTaskQuality deleteSaleTaskQualityPigType(Integer saletaskquality_id, Integer related_pigtype_id) {
		SaleTaskQuality saletaskquality = saleTaskQualityDAO.findSaleTaskQualityByPrimaryKey(saletaskquality_id, -1, -1);
		PigType related_pigtype = pigTypeDAO.findPigTypeByPrimaryKey(related_pigtype_id, -1, -1);

		saletaskquality.setPigType(null);
		related_pigtype.getSaleTaskQualities().remove(saletaskquality);
		saletaskquality = saleTaskQualityDAO.store(saletaskquality);
		saleTaskQualityDAO.flush();

		related_pigtype = pigTypeDAO.store(related_pigtype);
		pigTypeDAO.flush();

		pigTypeDAO.remove(related_pigtype);
		pigTypeDAO.flush();

		return saletaskquality;
	}

	/**
	 * Return all SaleTaskQuality entity
	 * 
	 */
	@Transactional
	public List<SaleTaskQuality> findAllSaleTaskQualitys(Integer startResult, Integer maxRows) {
		return new java.util.ArrayList<SaleTaskQuality>(saleTaskQualityDAO.findAllSaleTaskQualitys(startResult, maxRows));
	}

	/**
	 * Save an existing PigType entity
	 * 
	 */
	@Transactional
	public SaleTaskQuality saveSaleTaskQualityPigType(Integer id, PigType related_pigtype) {
		SaleTaskQuality saletaskquality = saleTaskQualityDAO.findSaleTaskQualityByPrimaryKey(id, -1, -1);
		PigType existingpigType = pigTypeDAO.findPigTypeByPrimaryKey(related_pigtype.getId());

		// copy into the existing record to preserve existing relationships
		if (existingpigType != null) {
			existingpigType.setId(related_pigtype.getId());
			existingpigType.setName(related_pigtype.getName());
			existingpigType.setMemo(related_pigtype.getMemo());
			related_pigtype = existingpigType;
		}

		saletaskquality.setPigType(related_pigtype);
		related_pigtype.getSaleTaskQualities().add(saletaskquality);
		saletaskquality = saleTaskQualityDAO.store(saletaskquality);
		saleTaskQualityDAO.flush();

		related_pigtype = pigTypeDAO.store(related_pigtype);
		pigTypeDAO.flush();

		return saletaskquality;
	}

	/**
	 */
	@Transactional
	public SaleTaskQuality findSaleTaskQualityByPrimaryKey(Integer id) {
		return saleTaskQualityDAO.findSaleTaskQualityByPrimaryKey(id);
	}

	/**
	 * Load an existing SaleTaskQuality entity
	 * 
	 */
	@Transactional
	public Set<SaleTaskQuality> loadSaleTaskQualitys() {
		return saleTaskQualityDAO.findAllSaleTaskQualitys();
	}

	/**
	 * Delete an existing Task entity
	 * 
	 */
	@Transactional
	public SaleTaskQuality deleteSaleTaskQualityTask(Integer saletaskquality_id, Integer related_task_id) {
		SaleTaskQuality saletaskquality = saleTaskQualityDAO.findSaleTaskQualityByPrimaryKey(saletaskquality_id, -1, -1);
		Task related_task = taskDAO.findTaskByPrimaryKey(related_task_id, -1, -1);

		saletaskquality.setTask(null);
		related_task.getSaleTaskQualities().remove(saletaskquality);
		saletaskquality = saleTaskQualityDAO.store(saletaskquality);
		saleTaskQualityDAO.flush();

		related_task = taskDAO.store(related_task);
		taskDAO.flush();

		taskDAO.remove(related_task);
		taskDAO.flush();

		return saletaskquality;
	}

	/**
	 * Delete an existing SaleTaskQuality entity
	 * 
	 */
	@Transactional
	public void deleteSaleTaskQuality(SaleTaskQuality saletaskquality) {
		saleTaskQualityDAO.remove(saletaskquality);
		saleTaskQualityDAO.flush();
	}

	/**
	 * Save an existing SaleTaskQuality entity
	 * 
	 */
	@Transactional
	public void saveSaleTaskQuality(SaleTaskQuality saletaskquality) {
		SaleTaskQuality existingSaleTaskQuality = saleTaskQualityDAO.findSaleTaskQualityByPrimaryKey(saletaskquality.getId());

		if (existingSaleTaskQuality != null) {
			if (existingSaleTaskQuality != saletaskquality) {
				existingSaleTaskQuality.setId(saletaskquality.getId());
				existingSaleTaskQuality.setPrice(saletaskquality.getPrice());
				existingSaleTaskQuality.setQuality(saletaskquality.getQuality());
				existingSaleTaskQuality.setMemo(saletaskquality.getMemo());
			}
			saletaskquality = saleTaskQualityDAO.store(existingSaleTaskQuality);
		} else {
			saletaskquality = saleTaskQualityDAO.store(saletaskquality);
		}
		saleTaskQualityDAO.flush();
	}

	/**
	 * Save an existing Task entity
	 * 
	 */
	@Transactional
	public SaleTaskQuality saveSaleTaskQualityTask(Integer id, Task related_task) {
		SaleTaskQuality saletaskquality = saleTaskQualityDAO.findSaleTaskQualityByPrimaryKey(id, -1, -1);
		Task existingtask = taskDAO.findTaskByPrimaryKey(related_task.getId());

		// copy into the existing record to preserve existing relationships
		if (existingtask != null) {
			existingtask.setId(related_task.getId());
			existingtask.setTaskNumber(related_task.getTaskNumber());
			existingtask.setCreateTime(related_task.getCreateTime());
			existingtask.setStartTime(related_task.getStartTime());
			existingtask.setEndTime(related_task.getEndTime());
			existingtask.setMemo(related_task.getMemo());
			related_task = existingtask;
		}

		saletaskquality.setTask(related_task);
		related_task.getSaleTaskQualities().add(saletaskquality);
		saletaskquality = saleTaskQualityDAO.store(saletaskquality);
		saleTaskQualityDAO.flush();

		related_task = taskDAO.store(related_task);
		taskDAO.flush();

		return saletaskquality;
	}

	/**
	 * Return a count of all SaleTaskQuality entity
	 * 
	 */
	@Transactional
	public Integer countSaleTaskQualitys() {
		return ((Long) saleTaskQualityDAO.createQuerySingleResult("select count(o) from SaleTaskQuality o").getSingleResult()).intValue();
	}
}
